/* $RCSFile: HttpUrlBuilder.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/HttpUrlBuilder.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:27  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
using System.Text;
using System.Threading;
using System.Configuration;
using System.Configuration.Provider;
using System.Web;
using System.Web.Configuration;
using System.Web.Caching;

using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Web;
using AddonNice.Configuration.Provider;
using AddonNice.Settings;
using AddonNice.CRMWrapper;

namespace AddonNice.Core
{
	/// <summary>
	/// HttpUrlBuilder
	/// This Class is in charge of building all the Urls in Addon Nice to prevent hardcoded urls. 
	/// This makes it easier to update urls across the multiple portals
	/// </summary>
	public class HttpUrlBuilder
	{

        private static UrlBuilderProvider _provider				=   null;
        private static UrlBuilderProviderCollection _providers	=   null;
        private static object _lock								=   new object();

        public static UrlBuilderProvider Provider
        {
            get 
            { 
                LoadProviders();
                return _provider; 
            }
        }


        private static void LoadProviders()
        {
            // Avoid claiming lock if providers are already loaded
            if (_provider == null)
            {
                if ( UrlBuilderTrace.Sw.TraceVerbose )
                    Trace.WriteLine("[V]HttpUrlBuilder LoadProviders BEG loading urlbuilder providers",UrlBuilderTrace.Sw.Info);
                lock (_lock)
                {
                    // Do this again to make sure _provider is still null
                    if (_provider == null)
                    {
                        // Get a reference to the <imageService> section
                        UrlServiceSection section	=   (UrlServiceSection)WebConfigurationManager.GetSection("UrlService");
                        // Load registered providers and point _provider to the default provider
                        _providers					=   new UrlBuilderProviderCollection();
                        ProvidersHelper.InstantiateProviders(section.Providers, _providers, typeof(UrlBuilderProvider));
                        _provider					=   _providers[section.DefaultProvider];
                        if (_provider == null)
                        {
                            string msg              =   string.Format("[E]HttpUrlBuilder LoadProviders Unable to load DefaultProvider: {0}",section.DefaultProvider);
                            if (UrlBuilderTrace.Sw.TraceError)
                                Trace.WriteLine(msg, UrlBuilderTrace.Sw.Info);
                            throw new ProviderException(msg);
                        }
                    }
                }
                if ( UrlBuilderTrace.Sw.TraceVerbose )
                    Trace.WriteLine("[V]HttpUrlBuilder LoadProviders END loaded urlbuilder provider",UrlBuilderTrace.Sw.Info);
            }
        }

		/// <summary> 
		/// Builds the url for get to current portal home page
		/// containing the application path, portal alias, page ID, and language. 
		/// </summary> 
		public static string BuildUrl()
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl DefaultPage: {0}", DefaultPage),UrlBuilderTrace.Sw.Info);
			return BuildUrl("~/" + DefaultPage,false,DynPageLoc.Normal, 0, 0, null, string.Empty, string.Empty,string.Empty);
		}

		/// <summary> 
		/// Builds the url for get to current portal home page
		/// containing the application path, portal alias, page ID, and language. 
		/// </summary> 
		/// <param name="targetPage">Linked page</param> 
		public static string BuildUrl(string targetPage)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}", targetPage),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage,false,DynPageLoc.Normal,  0, 0, null, string.Empty, string.Empty, string.Empty);
		}

		/// <summary> 
		/// Builds the url for get to current portal home page
		/// containing the application path, portal alias, page ID, and language. 
		/// </summary> 
		/// <param name="targetPage">Linked page</param> 
		/// <param name="customAttributes">Any custom attribute that can be needed</param> 
		public static string BuildUrl(string targetPage, string customAttributes)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, customAttributes: {1}", targetPage,customAttributes),UrlBuilderTrace.Sw.Info);
			if ( HttpContext.Current != null )
			{
				object ob	                        =	HttpContext.Current.Items[StStr.strPortalSettings];
				if ( ob != null)
				{
					PortalSettings currentSetting   =   (PortalSettings) ob;
					int pageID	                    =	currentSetting.ActivePageSettings.PageID;
                    if ( UrlBuilderTrace.Sw.TraceVerbose )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl found pageID: {0}",pageID),UrlBuilderTrace.Sw.Info);
					if ( pageID != 0 )
						return BuildUrl(targetPage,DynPageLoc.Normal, pageID, 0, null, customAttributes, string.Empty,string.Empty);
				}

			}
			return BuildUrl(targetPage,false,DynPageLoc.Normal, 0, 0, null, customAttributes, string.Empty,string.Empty);
		}

		public static string BuildPortalUrl(string portalAlias,int pageID)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildPortalUrl portalAlias: {0}, pageID: {1}",portalAlias, pageID),UrlBuilderTrace.Sw.Info);
			return BuildUrl("~/" + DefaultPage,false,DynPageLoc.Normal, pageID, 0, null,string.Empty,portalAlias, string.Empty);
		}

        /// <summary> 
		/// Takes a page ID and builds the url for get the desidered page 
		/// containing the application path, portal alias, page ID, and language. 
		/// </summary> 
		/// <param name="tabID">ID of the page</param> 
		public static string BuildUrl(int pageID)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl pageID: {0}", pageID),UrlBuilderTrace.Sw.Info);
			return BuildUrl("~/" + DefaultPage,false,DynPageLoc.Normal, pageID, 0, null, string.Empty, string.Empty, string.Empty);
		}

		public static string BuildUrl(DynPageLoc PageLoc, int pageID)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl pageID: {0}", pageID),UrlBuilderTrace.Sw.Info);
			return BuildUrl("~/" + DefaultPage,false,PageLoc,pageID, 0, null, string.Empty, string.Empty, string.Empty);
		}

        public static string BuildUrlWithUrlKeyWord(int pageID, string urlKeywords)
        {
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrlWithUrlKeyWord pageID: {0}, urlKeywords: {1}", pageID,urlKeywords),UrlBuilderTrace.Sw.Info);
            return BuildUrl("~/" + DefaultPage, DynPageLoc.Normal, pageID, 0, null, string.Empty, string.Empty, urlKeywords);
        }

        public static string BuildUrlWithUrlKeyWord(DynPageLoc PageLoc,int pageID, string urlKeywords)
        {
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrlWithUrlKeyWord pageID: {0}, urlKeywords: {1}, PageLoc: {2}", pageID,urlKeywords,PageLoc),UrlBuilderTrace.Sw.Info);
            return BuildUrl("~/" + DefaultPage, PageLoc, pageID, 0, null, string.Empty, string.Empty, urlKeywords);
        }
        
        public static string BuildUrlWithCustomAttributes(int pageID, string customAttributes)
        {
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrlWithCustomAttributes pageID: {0}, customAttributes: {1}", pageID,customAttributes),UrlBuilderTrace.Sw.Info);
            return BuildUrl("~/" + DefaultPage, DynPageLoc.Normal, pageID, 0, null, customAttributes, string.Empty, string.Empty);
        }

        public static string BuildUrlWithCustomAttributes(DynPageLoc PageLoc,int pageID, string customAttributes)
        {
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrlWithCustomAttributes pageID: {0}, customAttributes: {1}, PageLoc: {2}.", pageID,customAttributes,PageLoc),UrlBuilderTrace.Sw.Info);
            return BuildUrl("~/" + DefaultPage, PageLoc, pageID, 0, null, customAttributes, string.Empty, string.Empty);
        }

        public static string BuildUrlWithCustomAttributes(DynPageLoc PageLoc,int pageID, int moduleID,string customAttributes)
        {
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrlWithCustomAttributes pageID: {0}, moduleID: {1}, customAttributes: {2}, PageLoc: {3}.", 
                    new object[]{pageID,moduleID,customAttributes,PageLoc}),UrlBuilderTrace.Sw.Info);
            return BuildUrl("~/" + DefaultPage, PageLoc, pageID,moduleID,null,customAttributes, string.Empty, string.Empty);
        }

        /// <summary> 
		/// Takes a page ID and builds the url for get the desidered page (non default)
		/// containing the application path, portal alias, page ID, and language. 
		/// </summary> 
		/// <param name="targetPage">Linked page</param> 
		/// <param name="tabID">ID of the tab</param> 
		/// <param name="customAttributes">Any custom attribute that can be needed</param> 
		public static string BuildUrlWithCustomAttributes(string targetPage, int pageID, string customAttributes)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrlWithCustomAttributes targetPage: {0}, pageID: {1}, customAttributes: {2}",targetPage,pageID,customAttributes),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage, DynPageLoc.Normal, pageID, 0, null, customAttributes, string.Empty, string.Empty);
		}

		public static string BuildUrlWithCustomAttributes(string targetPage,DynPageLoc PageLoc,int pageID, string customAttributes)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrlWithCustomAttributes targetPage: {0}, pageID: {1}, customAttributes: {2}",targetPage,pageID,customAttributes),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage, PageLoc,pageID, 0, null, customAttributes, string.Empty, string.Empty);
		}

		public static string BuildUrlWithCustomAttributes(string targetPage,bool keepTargetPage,DynPageLoc PageLoc,int pageID, string customAttributes)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrlWithCustomAttributes targetPage: {0}, pageID: {1}, customAttributes: {2}",targetPage,pageID,customAttributes),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage,keepTargetPage, PageLoc,pageID, 0, null, customAttributes, string.Empty, string.Empty);
		}

		public static string BuildUrlWithCustomAttributes(string targetPage,bool keepTargetPage,DynPageLoc PageLoc,int pageID,int ModuleID,string customAttributes)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrlWithCustomAttributes targetPage: {0}, pageID: {1}, ModuleID: {2}, customAttributes: {3}",
                    new object[]{targetPage,pageID,ModuleID,customAttributes}),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage,keepTargetPage, PageLoc,pageID,ModuleID, null, customAttributes, string.Empty, string.Empty);
		}

        /// <summary> 
        /// Takes a page ID and builds the url for get the desidered page 
        /// containing the application path, portal alias, pageID, and language. 
        /// </summary> 
        /// <param name="tabID">ID of the tab</param> 
        /// <param name="urlKeywords">Add some keywords to uniquely identify this tab. Usual source is UrlKeyword from TabSettings.</param> 
        /// <param name="customAttributes">Any custom attribute that can be needed</param> 
		public static string BuildUrlWithCustomAttributesAndKeyWord(int pageID,string customAttributes,string urlKeywords)
        {
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrlWithCustomAttributesAndKeyWord pageID: {0}, customAttributes: {1}, urlKeywords: {2}", pageID,customAttributes,urlKeywords),UrlBuilderTrace.Sw.Info);
            return BuildUrl("~/" + DefaultPage, DynPageLoc.Normal, pageID, 0, null, customAttributes, string.Empty, urlKeywords);
        }

		public static string BuildUrlWithCustomAttributesAndKeyWord(DynPageLoc PageLoc, int pageID,string customAttributes,string urlKeywords)
        {
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrlWithCustomAttributesAndKeyWord pageID: {0}, customAttributes: {1}, urlKeywords: {2}", pageID,customAttributes,urlKeywords),UrlBuilderTrace.Sw.Info);
            return BuildUrl("~/" + DefaultPage, PageLoc,pageID, 0, null, customAttributes, string.Empty, urlKeywords);
        }

		/// <summary> 
		/// Takes a page ID and builds the url for get the desidered page (non default)
		/// containing the application path, portal alias, page ID, and language. 
		/// </summary> 
		/// <param name="targetPage">Linked page</param> 
		/// <param name="tabID">ID of the tab</param> 
		public static string BuildUrl(string targetPage, int pageID)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}",targetPage,pageID),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage, DynPageLoc.Normal, pageID, 0, null, string.Empty, string.Empty, string.Empty);
		}

		public static string BuildUrl(string targetPage,DynPageLoc PageLoc,  int pageID)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}",targetPage,pageID),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage, PageLoc,pageID, 0, null, string.Empty, string.Empty, string.Empty);
		}

        public static string BuildUrl(string targetPage,bool keepTargetPage,DynPageLoc PageLoc,  int pageID)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, keepTargetPage: {1}, PageLoc: {2}, pageID: {3}",
                    new object[]{targetPage,keepTargetPage,PageLoc,pageID}),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage,keepTargetPage, PageLoc,pageID, 0, null, string.Empty, string.Empty, string.Empty);
		}
		
        /// <summary> 
		/// Takes a page ID and builds the url for get the desidered page (non default)
		/// containing the application path, portal alias, page ID, and language. 
		/// </summary> 
		/// <param name="targetPage">Linked page</param> 
		/// <param name="tabID">ID of the tab</param> 
		/// <param name="customAttributes">Any custom attribute that can be needed</param> 
		[Obsolete("Obsolete: use BuildUrlWithCustomAttributes")]
		public static string BuildUrl(string targetPage, int pageID, string customAttributes)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}, customAttributes: {2}",targetPage,pageID,customAttributes),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage, DynPageLoc.Normal, pageID, 0, null, customAttributes, string.Empty, string.Empty);
		}

		[Obsolete("Obsolete: use BuildUrlWithCustomAttributes")]
		public static string BuildUrl(string targetPage,DynPageLoc PageLoc,  int pageID, string customAttributes)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}, customAttributes: {2}",targetPage,pageID,customAttributes),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage, PageLoc,pageID, 0, null, customAttributes, string.Empty, string.Empty);
		}

		[Obsolete("Obsolete: use BuildUrlWithCustomAttributes")]
		public static string BuildUrl(string targetPage,bool keepTargetPage,DynPageLoc PageLoc,  int pageID, string customAttributes)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}, customAttributes: {2}",targetPage,pageID,customAttributes),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage,keepTargetPage, PageLoc,pageID, 0, null, customAttributes, string.Empty, string.Empty);
		}

        /// <summary> 
		/// Takes a page ID and builds the url for get the desidered page (non default)
		/// containing the application path, portal alias, page ID, and language. 
		/// </summary> 
		/// <param name="targetPage">Linked page</param> 
		/// <param name="tabID">ID of the tab</param> 
		/// <param name="customAttributes">Any custom attribute that can be needed</param> 
		/// <param name="currentAlias">Current Portal Alias</param> 
		public static string BuildUrl(string targetPage, int pageID, string customAttributes, string currentAlias)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}, customAttributes: {2}, currentAlias: {3}",
                    new object[]{targetPage,pageID,customAttributes,currentAlias}),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage, DynPageLoc.Normal, pageID, 0, null, customAttributes, currentAlias, string.Empty);
		}

		public static string BuildUrl(string targetPage,DynPageLoc PageLoc, int pageID, string customAttributes, string currentAlias)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}, customAttributes: {2}, currentAlias: {3}",
                    new object[]{targetPage,pageID,customAttributes,currentAlias}),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage, PageLoc,pageID, 0, null, customAttributes, currentAlias, string.Empty);
		}

        /// <summary> 
		/// Takes a page ID, moduleID and builds the url for get the desidered page (non default)
		/// containing the application path, portal alias, page ID, and language. 
		/// </summary> 
		/// <param name="targetPage">Linked page</param> 
		/// <param name="tabID">ID of the tab</param> 
		/// <param name="customAttributes">Any custom attribute that can be needed</param> 
		/// <param name="currentAlias">Current Portal Alias</param> 
		public static string BuildUrl(string targetPage, int pageID, int ModuleID,string customAttributes, string currentAlias)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}, ModuleID: {2}, customAttributes: {3}, currentAlias: {4}",
                    new object[]{targetPage,pageID,ModuleID,customAttributes,currentAlias}),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage,DynPageLoc.Normal, pageID, ModuleID, null, customAttributes, currentAlias, string.Empty);
		}

		public static string BuildUrl(string targetPage, DynPageLoc PageLoc, int pageID, int ModuleID,string customAttributes, string currentAlias)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}, ModuleID: {2}, customAttributes: {3}, currentAlias: {4}",
                    new object[]{targetPage,pageID,ModuleID,customAttributes,currentAlias}),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage, PageLoc, pageID, ModuleID, null, customAttributes, currentAlias, string.Empty);
		}
        
        /// <summary> 
		/// Takes a page ID and builds the url for get the desidered page (non default)
		/// containing the application path, portal alias, page ID, and language. 
		/// </summary> 
		/// <param name="targetPage">Linked page</param> 
		/// <param name="tabID">ID of the tab</param> 
		/// <param name="culture">Client culture</param> 
		/// <param name="customAttributes">Any custom attribute that can be needed</param> 
		/// <param name="currentAlias">Current Portal Alias</param> 
		public static string BuildUrl(string targetPage, int pageID, CultureInfo culture, string customAttributes, string currentAlias)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}, culture: {2}, customAttributes: {3}, currentAlias: {4}",
                    new object[]{targetPage,pageID,culture.ToString(),customAttributes,currentAlias}),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage, DynPageLoc.Normal,pageID, 0, culture, customAttributes, currentAlias, string.Empty);
		}

		public static string BuildUrl(string targetPage,DynPageLoc PageLoc, int pageID, CultureInfo culture, string customAttributes, string currentAlias)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}, culture: {2}, customAttributes: {3}, currentAlias: {4}",
                    new object[]{targetPage,pageID,culture.ToString(),customAttributes,currentAlias}),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage,PageLoc, pageID, 0, culture, customAttributes, currentAlias, string.Empty);
		}

        /// <summary> 
		/// Takes a page ID and builds the url for get the desidered page (non default)
		/// containing the application path, portal alias, page ID, and language. 
        /// For CRM support, we add systematicaly the 'CRM' query string composer of typename,id and type
		/// </summary> 
		/// <param name="targetPage">Linked page</param> 
		/// <param name="tabID">ID of the tab</param> 
		/// <param name="modID">ID of the module</param>
		public static string BuildUrl(string targetPage,int pageID, int modID)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}, modID: {2}",targetPage,pageID,modID),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage, DynPageLoc.Normal,pageID, modID, null, string.Empty, string.Empty, string.Empty);
		}

		public static string BuildUrl(string targetPage,DynPageLoc PageLoc, int pageID, int modID)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}, modID: {2}",targetPage,pageID,modID),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage, PageLoc, pageID, modID, null, string.Empty, string.Empty, string.Empty);
		}

		public static string BuildUrl(string targetPage,bool keepTargetPage,DynPageLoc PageLoc, int pageID, int modID)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl targetPage: {0}, pageID: {1}, modID: {2}",targetPage,pageID,modID),UrlBuilderTrace.Sw.Info);
			return BuildUrl(targetPage,keepTargetPage, PageLoc, pageID, modID, null, string.Empty, string.Empty, string.Empty);
		}

		public static string BuildUrl(string targetPage,DynPageLoc PageLoc, int tabID, int modID, CultureInfo culture, string customAttributes, string currentAlias, string urlKeywords)
		{
            return BuildUrl(targetPage,false,PageLoc,tabID,modID,culture,customAttributes,currentAlias,urlKeywords);
        }

        /// <summary> 
		/// Takes a page ID and builds the url for get the desidered page (non default)
		/// containing the application path, portal alias, page ID, and language. 
        /// When the targetPage param starts with ~/, build a full path url with scheme according PortalIsSecure portal setting
        /// So to be sure to have the correct http scheme, use a target page starting with ~/
		/// </summary> 
		/// <param name="targetPage">Linked page</param> 
		/// <param name="keepTargetPage">keep sent page as requested page</param> 
		/// <param name="PageLoc">Where this page should be loaded</param> 
		/// <param name="tabID">ID of the Page</param> 
		/// <param name="modID">ID of the module</param> 
		/// <param name="culture">Client culture</param> 
		/// <param name="customAttributes">Any custom attribute that can be needed. Use the following format...single attribute: paramname--paramvalue . Multiple attributes: paramname--paramvalue/paramname2--paramvalue2/paramname3--paramvalue3 </param> 
		/// <param name="currentAlias">Portal Alias</param> 
		/// <param name="urlKeywords">Add some keywords to uniquely identify this tab. Usual source is UrlKeyword from TabSettings.</param> 
		public static string BuildUrl(string targetPage,bool keepTargetPage,DynPageLoc PageLoc, int tabID, int modID, CultureInfo culture, string customAttributes, string currentAlias, string urlKeywords)
		{
            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl BEG targetPage: [{0}], tabID: [{1}], modID: [{2}], culture: [{3}], customAttributes: [{4}], currentAlias: [{5}], urlKeywords: [{6}]",
                    new object[]{targetPage.ToString(),tabID, modID,
                        (culture == null) ? string.Empty:culture.ToString(),
                        customAttributes,
                        currentAlias.ToString(),urlKeywords.ToString()}),UrlBuilderTrace.Sw.Info);
            // CS 9/01/07 let allow VS designer !
            if (HttpContext.Current == null)
                return string.Empty;
            PortalSettings currentSetting   =   null;
			object ob                       =   HttpContext.Current.Items[StStr.strPortalSettings];
            if ( ob != null)
				currentSetting              =   (PortalSettings) ob;

            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl currentSetting: {0}",(currentSetting != null )),UrlBuilderTrace.Sw.Info);
            // For CRM support, we add systematically the 'CRM' query string composed of typename,id and type+ lcids
            // we must rebuild the customAttributes string
            if ( PortalSettings.IsUnderCRM )
            {
                NameValueCollection v       =   new NameValueCollection();
                bool hasQ                   =   false;
                if (!string.IsNullOrEmpty(customAttributes))
                {
                    string tmpStr           =   HttpContext.Current.Server.UrlDecode(customAttributes);
                    if (tmpStr[0] == '?')
                    {
                        tmpStr              =   tmpStr.Substring(1);
                        hasQ                =   true;
                    }
                    string[] strAr          =   tmpStr.Split('&');
                    int nb                  =   strAr.GetUpperBound(0) + 1;
                    for (int i = 0; i < nb; i++)
                    {
                        string[] strAtt     =   strAr[i].Split('=');
                        if (strAtt.GetUpperBound(0) == 1)
                             v.Add(strAtt[0], strAtt[1]);
                        else v.Add(strAr[i], string.Empty);
                    }
                }
                // v3
                v[CrmStr.strCrmTypeName]    =     PortalSettings.RequestEntityTypename;
                v[CrmStr.strCrmID]          =     PortalSettings.RequestEntityIDStr;
                v[CrmStr.strCrmType]        =     PortalSettings.RequestEntityTypeCode;
                // v4
                v[CrmStr.strOrgName]        =     PortalSettings.RequestOrgName;
		        v[CrmStr.strUserLCID]       =     PortalSettings.RequestUserLCID;
		        v[CrmStr.strOrgLCID]        =     PortalSettings.RequestOrgLCID;

                StringBuilder b             =   hasQ ? new StringBuilder("?") : new StringBuilder();
                bool prem                   =   true;
                foreach(string key in v.AllKeys)
                {
                    if (prem)
                    {
                        if (string.IsNullOrEmpty(v[key]))
                             b.AppendFormat("{0}", key);
                        else b.AppendFormat("{0}={1}", key, v[key]);
                        prem                =   false;
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(v[key]))
                             b.AppendFormat("&{0}", key);
                        else b.AppendFormat("&{0}={1}", key, v[key]);
                    }
                }
                customAttributes            =   b.ToString();
                if ( UrlBuilderTrace.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl under CRM CustomAttributes: '{0}'", customAttributes),UrlBuilderTrace.Sw.Info);
            }
            if (culture == null)
			{
				if (currentSetting != null)
					culture                 =   currentSetting.PortalContentLanguage;
				else
					culture                 =   Thread.CurrentThread.CurrentUICulture;
			}

			if ( string.IsNullOrEmpty(currentAlias) )
			{
				if ( currentSetting != null)
					currentAlias            =   currentSetting.BaseAlias;
				else
                    currentAlias            =   Config.DefaultPortal;
			}
			// prepare for additional querystring values
			string completeCustomAttributes =  customAttributes;

            if ( UrlBuilderTrace.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HttpUrlBuilder BuildUrl calling provider targetPage: [{0}], keepTargetPage: {1}, tabID: {2}, modID: {3}, culture: {4}, customAttributes: [{5}], currentAlias: [{6}], urlKeywords: [{7}]",
                    new object[]{targetPage.ToString(),keepTargetPage,tabID, modID,culture.ToString(),customAttributes,currentAlias.ToString(),urlKeywords.ToString()}),UrlBuilderTrace.Sw.Info);
            return Provider.BuildUrl(targetPage,keepTargetPage,PageLoc, tabID, modID, culture, completeCustomAttributes, currentAlias, urlKeywords);	
		}
	
		public static string DefaultPage
		{
			get
			{
                return Provider.DefaultPage;		
			}
		}

		public static string DefaultSplitter
		{
			get
			{
                return Provider.DefaultSplitter;		
			}
		}

		/// <summary> 
		/// Builds the url for get to current portal home page
		/// containing the application path, portal alias, page ID, and language. 
		/// </summary> 
		public static string UrlKeyword(int tabID)
		{
            return Provider.UrlKeyword(tabID);		
		}

		/// <summary> 
		/// Returns the page name that has been specified. 
		/// </summary> 
		public static string UrlPageName(int tabID)
		{
            return Provider.UrlPageName(tabID);	
		}

		/// <summary> 
		/// Determines if a tab is simply a placeholder in the navigation
		/// </summary> 
		public static bool IsPlaceholder(int tabID)
		{
            return Provider.IsPlaceholder(tabID);
		}

		/// <summary> 
		/// Returns the URL for a tab that is a link only.
		/// </summary> 
		public static string TabLink(int tabID)
		{
            return Provider.TabLink(tabID);
		}

		/// <summary> 
		/// Clears any Url Elements e.g IsPlaceHolder, TabLink, UrlKeywords and PageName etc
		/// that may be stored (either in cache, xml etc depending on the UrlBuilder implementation  
		/// </summary> 
		public static void Clear(int tabID)
		{
            Provider.Clear(tabID);		
		}

	}
}
